﻿using System;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using ExamSchedulerProject.Util;
using Microsoft.Win32;

namespace ExamSchedulerProject
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {

            InitializeComponent();
            String username = Properties.Settings.Default["Username"].ToString();
            String password = Properties.Settings.Default["Password"].ToString();
            String studentCode = Properties.Settings.Default["StudentCode"].ToString();
            if (String.IsNullOrEmpty(username) || String.IsNullOrEmpty(password))
            {
                MessageBox.Show("Please inform your CMS account !");
                if (String.IsNullOrEmpty(username))
                {
                    txtUsername.Focus();
                }
                else if (String.IsNullOrEmpty(password))
                {
                    studentCode = CheckUsername(username);
                    if(!String.IsNullOrEmpty(studentCode))
                    {
                        txtUsername.Text = username;
                        txtStudentCode.Text = studentCode;
                    }
                    
                    txtPassword.Focus();
                }
            }
            else
            {
                txtUsername.Text = username;
                //MessageBox.Show("Password: " + EncryptionData.Decrypt(password, studentCode.ToUpper()));
                txtPassword.Password = EncryptionData.Decrypt(password, studentCode.ToUpper());
                txtStudentCode.Text = studentCode;
            }
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            // Create
            OpenFileDialog dialog = new OpenFileDialog();
            // Set filter
            dialog.InitialDirectory = @"E:\FPT\Linhtinh";
            dialog.Filter = "Excel 97 - 2003 workbook (*.xls)|*.xls|Excel workbook (*.xlsx)|*.xlsx";
            // Display
            Nullable<bool> result = dialog.ShowDialog();
            // Get the selected file name and display in textbox
            if (result == true)
            {
                // Open document
                string filename = dialog.FileName;
                txtFilename.Text = filename;
            }

        }

        private void TabItem_ContextMenuOpening(object sender, ContextMenuEventArgs e)
        {

        }

        private void btnSave_Click(object sender, RoutedEventArgs e)
        {
            String username = txtUsername.Text;
            String password = txtPassword.Password;
            String studentCode = null;
            if (!String.IsNullOrEmpty(username) && !String.IsNullOrEmpty(password))
            {
                studentCode = CheckUsername(username);

                if (!String.IsNullOrEmpty(studentCode))
                {
                    Properties.Settings.Default["Username"] = username;password = EncryptionData.Encrypt(password, studentCode.ToUpper());
                    Properties.Settings.Default["Password"] = password;
                    Properties.Settings.Default["StudentCode"] = studentCode.ToUpper();
                    Properties.Settings.Default.Save();
                    txtStudentCode.Text = studentCode.ToUpper();
                    MessageBox.Show("Your Account Info is saved successfully.");
                }else
                {
                    MessageBox.Show("Your Username is not available.");
                    txtUsername.Focus();
                }
            }
        }
        private string CheckUsername(String username)
        {
            String studentCode;
            char f = username.ToUpper().ElementAt(username.Length - 7);
            char s = username.ToUpper().ElementAt(username.Length - 6);
            if ((f.Equals('S') && (s.Equals('E') || s.Equals('B'))) || (f.Equals('F') && s.Equals('B')))
            {
                studentCode = username.Substring(username.Length - 7);
            }
            else
            {
                studentCode = username.Substring(username.Length - 5);
            }
            int test;
            String subString = studentCode.Substring(studentCode.Length - 5);
            bool isNumber = int.TryParse(subString, out test);
            if (!isNumber)
            {
                studentCode = null;
            }
            return studentCode;
        }


    }
}
